var selAllBusArr = [];//列出的所有数据
var selBusArr = [];//当前被选中的所有数据
/**
显示或隐藏已选业务名称区域
**/
var showOrHideSelBusName = function(busName){
  var $selBusName = $("#selBusName");
  if(busName){
    $selBusName.html(busName);
  }else{
    $selBusName.empty();
  }
};

var setAllBusArr = function(){
  if(selAllBusArr.length === 0){
    var themeArr = $("#themeArr").val();
    if(themeArr){
        themeArr = JSON.parse(themeArr);
        if(themeArr){
            var len = themeArr.length;
            for(var i=0;i<len;i++){
                selAllBusArr.push(themeArr[i]);
            }
        }
    }
  }
};

/**
全选或者全不选事件处理
**/
var allCheck = function(){
  console.log($("#themeArr").val());
  
  var $allCheckbox = $("input[name='ids']");
  var $submitBtn = $("#submit-btn");
  if($("#allCheck").is(":checked")) {

    setAllBusArr();

    $allCheckbox.prop("checked","true");
    $submitBtn.prop("disabled", false);
    var selAllBusArrLen = selAllBusArr.length;
    var selBusArrLen = selBusArr.length;
    selBusArr = [];
    if(selBusArrLen !== selAllBusArrLen){
      for(var i=0;i<selAllBusArrLen;i++){
        selBusArr.push(selAllBusArr[i]);
      }
    }
  }else{
    selBusArr = [];
    $allCheckbox.removeAttr("checked");
    $submitBtn.prop("disabled", true);
  }
  showBusinessNames();
};
/**
根据当前业务名称是否被选中动态显示在业务名称区域
**/
var showBusinessNames = function(){
  var len = selBusArr.length;
  var names = "";
  for(var i=0;i<len;i++){
    names += selBusArr[i].name;
    if(i !== (len-1)){
      names += "，";
    }
  }
  showOrHideSelBusName(names);
};
/**
绑定业务数据多选框点击事件
**/
var bindCheckboxEvent = function(){
  var $allCheckbox = $("input[name='ids']");
  $allCheckbox.each(function(){
    $(this).click(function(){
      var $thisObj = $(this);
      var busId = $thisObj.val();
      var busName = $thisObj.siblings("span").html();
      var $allCheck = $("#allCheck");
      var $submitBtn = $("#submit-btn");
      var selObj = {id:busId,name:busName};
      if($(this).is(":checked")){
        selBusArr.push(selObj);
        var selBusArrLen = selBusArr.length;
        if(selBusArr.length === selAllBusArr.length){
          $allCheck.prop("checked","true");
        }
        $submitBtn.prop("disabled", false);
      }else{
        selBusArr.remove(selObj);
        $allCheck.removeAttr("checked");
        if(selBusArr.length === 0){
          $submitBtn.prop("disabled", true);
        }
      }
      showBusinessNames();
    })
  });
};

var toBusinessList = function(){
  window.location.href = base + '/spreadMemberAuthConfig/themeList';
};

$(function() {
  console.log('------begin---------');
  $("#submit-btn").prop("disabled", true);

  bindCheckboxEvent();

  $("#submit-btn").click(function(){
    var $submitBtn = $(this);
    var $allCheckbox = $("input[name='ids']:checked");
    var ids = $allCheckbox.serialize();
    var flowId = $("#flowId").val();
    var param = "config_business=10000&"+ids+"&flowId="+flowId;
    console.log(param);
    $.ajax({
      url : base + '/spreadMemberAuthConfig/saveConfigData',
      data : param,
      dataType : "json",
      beforeSend : function(data) {
        $submitBtn.prop("disabled", true);
      },
      success : function(data) {
        $submitBtn.prop("disabled", false);
        if(data.status == "success") {
          if(confirm("提交成功，确定则立即跳到列表页面查看结果，取消则留在本页面继续配置，确定吗？")){
            // 跳转到列表页面
            toBusinessList();
          }
        }else{
          sendMessage({
            title : "提交失败！",
            text : data.message,
            type : data.status,
            timer : 2000,
            showConfirmButton : true
          });
        }
      }
    });
  });
});